package com.baiyang.generator.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baiyang.generator.conf.Configuration;
import com.baiyang.generator.conf.DatabaseConfig;
import com.baiyang.generator.db.InitDB;
import com.baiyang.generator.entity.ColumnEntity;
import com.baiyang.generator.entity.ColumnInfo;
import com.baiyang.generator.entity.Variable;

public class DealData {
	private final static String VARIABLES = "variables";
	private final static String COLUMN_ENTITY = "columnEntity";
	private final static String CONFIGURATION = "Configuration";
	
	private static Configuration configuration = null;
	
	/**
	 * 可修改内容
	 */
	private static String db = "reading-platform";
	private static String tableName = "item_u";
	private static String path = "H:\\project\\ReadingPlatform\\baiyang-server";
	private static String projectPath = path + "\\src\\main\\java\\com\\baiyang\\server";
	private static String packageProject = "com.baiyang.server";
	private static String pathMapper = path + "\\src\\main\\resources\\com\\baiyang\\mapper";
	
	/**
	 * 设置数据库
	 * @return
	 */
	private static DatabaseConfig createDatabaseConfig() {
        DatabaseConfig config = DatabaseConfig.build();
        config.setDriver("com.mysql.cj.jdbc.Driver");
        config.setUrl("jdbc:mysql://localhost:3306/" + db + "?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC");
        config.setUserName("root");
        config.setPassword("password");
        config.setTablename(tableName);
        return config;
	}
	
	private static Configuration createConfiguration(String tableName,String tableRemark) {
		Configuration configuration = new Configuration();
		configuration.setProjectPath(projectPath);
		configuration.setPackageProject(packageProject);
		configuration.setPathMapper(pathMapper);
		configuration.setData(tableName, tableRemark);
		DealData.configuration = configuration;
		return configuration;
	}
	public static Map<String, Object> createMap() throws Exception{
		DatabaseConfig config = createDatabaseConfig();
		InitDB db = new InitDB();
		if(db.initConnection()) {
			Map<String, Object> map = new HashMap<String, Object>();
  			String tableName = config.getTablename();
			String tableRemark = db.getTableRemark(tableName);
			Configuration configuration = createConfiguration(tableName, tableRemark);
			map.put(CONFIGURATION, configuration);
			List<ColumnInfo> columnInfos = db.getColumnInfos(tableName,db.getPrimaryKey(tableName),db.getTableRemark(tableName));
			//设置entity
			List<Variable> list = new ArrayList<Variable>();
			columnInfos.forEach(e->{
				Variable variable = new Variable();
				variable.setType(e.getPropertyType());
				variable.setName(e.getPropertyName());
				variable.setRemark(e.getRemarks());
				variable.setNullable(e.getNullable());
				list.add(variable);
			});
			map.put(VARIABLES,list);
			//设置mapper
			List<ColumnEntity> columnEntitys = new ArrayList<ColumnEntity>();
			columnInfos.forEach(e->{
				ColumnEntity columnEntity = new ColumnEntity();
				columnEntity.setColumn(e.getColumnName());
				columnEntity.setVariable(e.getPropertyName());
				columnEntity.setType(e.getPropertyType());
				columnEntity.setNullable(e.getNullable());
				columnEntitys.add(columnEntity);
			});
			map.put(COLUMN_ENTITY, columnEntitys);
			return map;
		}else {
			return null;
		}
	}

	public static Configuration getConfiguration() {
		return configuration;
	}

	public static void setConfiguration(Configuration configuration) {
		DealData.configuration = configuration;
	}
}
